<?php
/**
 * Created by PhpStorm.
 * 阿里云移动推送
 * User: caicm
 * Date: 2017/7/29
 * Time: 16:43
 */

namespace WMApi\Service;

use WMApi\Model\WmAliyunPushModel;


class AliyunPushService {
    protected $push_aliyun;
    public function __construct() {
        import('Vendor.AliyunPush.AliyunPush');
        $this->push_aliyun = new \AliyunPush();
    }

    /**
     * APP推送
     * Created By caicm
     * @param int $seller_id 商家id
     * @param string $title 标题
     * @param string $body 内容
     * @return mixed
     */
    public function app_push($seller_id=0, $title='', $body=''){
        $push_sn = $this->get_push_list($seller_id);
        $ExtParameters['ios'] = array('mutable-content' => 1);
        $res = $this->push_aliyun->push($push_sn, $title, $body, 1, $ExtParameters);
        return $res->MessageId;
    }

    /**
     * 查询消息发送状态
     * Created By caicm
     * @param int $message_id
     * @return mixed
     */
    public function query_push_state($message_id=0){
        return $this->push_aliyun->query_push_state($message_id);
    }

    /**
     * 获取商家设备id
     * Created By caicm
     * @param $seller_id
     * @return array
     */
    protected function get_push_list($seller_id){
        $push_model = new WmAliyunPushModel();
        $push_info = $push_model->find_push_info($seller_id);
        if($push_info['ios_device_id']){
            $device_sn[] = $push_info['ios_device_id'];
        }
        if($push_info['android_device_id']){
            $device_sn[] = $push_info['android_device_id'];
        }
        return $device_sn;
    }

}